<?php
umask(0);
$pid = pcntl_fork();
if ($pid>0){
    exit(0);
}

if (posix_setsid()==-1){
    fprintf(STDOUT,"setsid失败\n");
}
$pid = pcntl_fork();
if ($pid>0){
    exit(0);
}

chdir("./");
fclose(STDIN);
fclose(STDOUT);
fclose(STDERR);

$stdin = fopen("/dev/null","a+");
$stdout = fopen("/dev/null","a+");
$stderr = fopen("/dev/null","a+");

echo "hello.x";

$fd = fopen("./demo28.log","a+");
$pid = pcntl_fork();
if ($pid==0){
    fprintf($fd,"pid=%d,ppid=%d,sid=%d,time=%s\n",posix_getpid(),posix_getppid(),posix_getsid(posix_getpid()),time());
    while (1){
        sleep(1);
    }
    exit(0);;
}

$pid = pcntl_wait($status);
if ($pid>0){
    fprintf($fd,"exit pid=%d,ppid=%d,sid=%d,time=%s\n",$pid,posix_getpid(),posix_getsid(posix_getpid()),time());
    fclose($fd);
    exit(0);
}